import React from "react";
import Taro from "@tarojs/taro";
import qs from "qs";
import cache from "./navigateCache";
import Navigator from "./navigator";

/**
 * 将缓存在 cache 对象当中的数据赋予给 props
 * @param Target
 */
//
export default function withRouterfunction(Target: any) {
  return class PLoader extends React.Component<any, any> {

    render() {
      // 页面路由参数
      const params = qs.parse(decodeURIComponent(decodeURIComponent(qs.stringify(Taro.getCurrentInstance().router?.params || {}))));
      // 页面间传递的数据
      const cacheKey = Navigator.getCacheKey();
      const data = cache.get(cacheKey);
      // 重置，预防引用导致内存泄漏
      cache.set(cacheKey, null);

      const router = { data: data, params: params};

      return <Target router={router} {...this.props} />
    }
  }
}


